package com.vhall.component.controller.inav;

import com.alibaba.fastjson.JSONObject;
import com.vhall.component.controller.inav.facade.InavFacade;
import com.vhall.component.controller.room.facade.RoomInnerFacade;
import com.vhall.component.entity.ctrl.vo.ConnectQueueAddReqVo;
import com.vhall.component.entity.inav.vo.*;
import com.vhall.component.entity.room.dto.RoomsEntityDTO;
import com.vhall.component.entity.room.vo.ReportGuideShownRequestVO;
import com.vhall.component.framework.common.annotation.VhallComponent;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.annotation.TokenAuth;
import com.vhall.component.plugin.common.annotation.VssTokenAuth;
import com.vhall.component.plugin.common.constant.RoomConstant;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.plugin.common.support.Token;
import com.vhall.component.plugin.common.support.TokenThreadLocal;
import com.vhall.component.plugin.common.support.VssToken;
import com.vhall.component.plugin.common.support.VssTokenThreadLocal;
import com.vhall.component.service.inav.ApplyService;
import com.vhall.component.service.inav.InavService;
import com.vhall.component.service.inav.InfoService;
import com.vhall.component.service.inav.InviteService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author lsl
 * @since 2021/6/7
 */
@VhallComponent
public class InavController implements InavFacade {

    @Autowired
    private RoomInnerFacade roomInnerFacade;

    @Autowired
    private InavService inavService;

    @Autowired
    private ApplyService applyService;

    @Autowired
    private InfoService infoService;

    @Autowired
    private InviteService inviteService;

    @Override
    @TokenAuth
    public Boolean queueAdd(ConnectQueueAddReqVo vo) {
        Token account = TokenThreadLocal.getToken();
        Integer accountId = account.getAccountId();
        return inavService.queueAdd(accountId, vo.getIlId());
    }

    @Override
    @VssTokenAuth
    public GetOnlineListRspVO getOnlineList(GetOnlineListReqVO reqVO) {
        if (reqVO.getPage() == null) {
            reqVO.setPage(1);
        }
        if (reqVO.getPagesize() == null) {
            reqVO.setPagesize(10);
        }
        return roomInnerFacade.getOnlineList(reqVO);

    }

    @Override
    @VssTokenAuth
    public Boolean setDevice(SetDeviceReqVO reqVO) {
        return inavService.setDevice(reqVO);
    }

    @Override
    @VssTokenAuth
    public Boolean setHandSup(SetHandSupReqVO reqVO) {
        VssToken token = VssTokenThreadLocal.getToken();
        return inavService.setHandSup(reqVO, token);
    }

    @Override
    @VssTokenAuth
    public void setDesktop(SetDesktopReqVO reqVO) {
        VssToken token = VssTokenThreadLocal.getToken();
        inavService.setDesktop(reqVO, token);
    }

    @Override
    @VssTokenAuth
    public UserStatusRspVO getUserStatus(GetUserStatusReqVO reqVO) {
        return inavService.getUserStatus(reqVO);
    }

    @Override
    @VssTokenAuth
    public void setAllBanned(SetAllBannedReqVO reqVO) {
        VssToken token = VssTokenThreadLocal.getToken();
        inavService.setAllBanned(reqVO, token);
    }

    @Override
    @VssTokenAuth
    public Integer setBanned(SetBannedReqVO reqVO) {
        return inavService.setBanned(reqVO, VssTokenThreadLocal.getToken());
    }

    @Override
    @VssTokenAuth
    public Boolean setKicked(SetKickedReqVO reqVO) {
        return inavService.setKicked(reqVO, VssTokenThreadLocal.getToken());
    }

    @Override
    @VssTokenAuth
    public BannedAndKickedListRspVO getBannedList(BannedKickedReqVO reqVO) {
        checkRoomIsRight(reqVO.getRoomId());
        if (reqVO.getPage() == null) {
            reqVO.setPage(1);
        }
        if (reqVO.getPagesize() == null) {
            reqVO.setPagesize(10);
        }
        return roomInnerFacade.getBannedList(reqVO);
    }

    @Override
    @VssTokenAuth
    public BannedKickedListRspVO getBannedList2(BannedKickedReqVO reqVO) {
        checkRoomIsRight(reqVO.getRoomId());
        if (reqVO.getPage() == null) {
            reqVO.setPage(1);
        }
        if (reqVO.getPagesize() == null) {
            reqVO.setPagesize(10);
        }
        return roomInnerFacade.getBannedList2(reqVO);
    }

    @Override
    @VssTokenAuth
    public BannedAndKickedListRspVO getKickedList(BannedKickedReqVO reqVO) {
        checkRoomIsRight(reqVO.getRoomId());
        if (reqVO.getPage() == null) {
            reqVO.setPage(1);
        }
        if (reqVO.getPagesize() == null) {
            reqVO.setPagesize(10);
        }
        return roomInnerFacade.getKickedList(reqVO);
    }

    @Override
    @VssTokenAuth
    public BannedKickedListRspVO getKickedList2(BannedKickedReqVO reqVO) {
        checkRoomIsRight(reqVO.getRoomId());
        if (reqVO.getPage() == null) {
            reqVO.setPage(1);
        }
        if (reqVO.getPagesize() == null) {
            reqVO.setPagesize(10);
        }
        return roomInnerFacade.getKickedList2(reqVO);
    }

    @Override
    @VssTokenAuth
    public Boolean setDeviceStatus(SetDeviceStatusReqVO reqVO) {
        RoomsEntityDTO room = checkRoomIsRight(reqVO.getRoomId());
        if (room.getStatus() != RoomConstant.STATUS_START) {
            throw new BusinessException(BizErrorCode.BIZ_NOT_START);
        }
        VssToken token = VssTokenThreadLocal.getToken();
        return inavService.setDeviceStatus(reqVO, token, room);
    }

    @Override
    @VssTokenAuth
    public Boolean setMainScreen(SetMainScreenReqVO reqVO) {
        return inavService.setMainScreen(reqVO, VssTokenThreadLocal.getToken());
    }

    @Override
    @VssTokenAuth
    public Boolean speak(SpeakReqVO reqVO) {
        // 1、检查房间信息是否存在。
        RoomsEntityDTO room = roomInnerFacade.getRoomInfo(reqVO.getRoomId());
        if (RoomConstant.STATUS_START != room.getStatus()) {
            throw new BusinessException(BizErrorCode.BIZ_NOT_START);
        }
        // 2、获取用户信息
        VssToken tokenInfo = VssTokenThreadLocal.getToken();
        inavService.speak(reqVO, tokenInfo);
        return true;
    }

    @Override
    @VssTokenAuth
    public Boolean noSpeak(NoSpeakReqVO reqVO) {
        //获取用户信息
        VssToken tokenInfo = VssTokenThreadLocal.getToken();
        return inavService.noSpeak(reqVO, tokenInfo);
    }

    @Override
    @VssTokenAuth
    public Boolean invite(InviteReqVO reqVO) {
        VssToken token = VssTokenThreadLocal.getToken();
        return inviteService.invite(reqVO, token);
    }

    @Override
    @VssTokenAuth
    public Boolean rejectInvite(InviteReqVO reqVO) {
        VssToken token = VssTokenThreadLocal.getToken();
        //获取房间信息
        RoomsEntityDTO room = checkRoomIsRight(reqVO.getRoomId());
        //拒绝上麦邀请
        return inviteService.rejectInvite(room, token, reqVO);
    }

    @Override
    @VssTokenAuth
    public Boolean agreeInvite(InviteReqVO reqVO) {
        VssToken token = VssTokenThreadLocal.getToken();
        //获取房间信息
        RoomsEntityDTO room = checkRoomIsRight(reqVO.getRoomId());
        return inviteService.agreeInvite(room, token, reqVO);
    }

    @Override
    @VssTokenAuth
    public Boolean cancelApply(CancelApplyReqVO reqVO) {
        // 1、获取用户信息
        VssToken tokenInfo = VssTokenThreadLocal.getToken();
        return applyService.cancelApply(reqVO, tokenInfo);
    }

    @Override
    @VssTokenAuth
    public Boolean apply(ApplyReqVO reqVO) {
        // 1、获取用户信息
        VssToken tokenInfo = VssTokenThreadLocal.getToken();
        return applyService.apply(reqVO, tokenInfo);
    }

    @Override
    @VssTokenAuth
    public Boolean agreeApply(AgreeApplyReqVO reqVO) {
        // 1、获取用户信息
        VssToken tokenInfo = VssTokenThreadLocal.getToken();
        return applyService.agreeApply(reqVO, tokenInfo);
    }

    @Override
    @VssTokenAuth
    public Boolean rejectApply(RejectApplyReqVO reqVO) {
        // 1、获取用户信息
        VssToken tokenInfo = VssTokenThreadLocal.getToken();
        return applyService.rejectApply(reqVO, tokenInfo);
    }

    @Override
    @VssTokenAuth
    public Boolean setDocPermission(SetDocPermissionReqVO reqVO) {
        // 1、获取用户信息
        VssToken token = VssTokenThreadLocal.getToken();
        return inavService.setDocPermissionAction(reqVO, token);
    }

    @Override
    @VssTokenAuth
    public List<GetUserListRspVO> getUserList(GetUserListReqVO reqVO) {
        UserListPage<GetUserListRspVO> userList = roomInnerFacade.getUserList(reqVO);
        return userList.getData();
    }

    @Override
    @TokenAuth(check = false)
    public InavRespVO get(GetInavVO reqVO) {
        // /v4/inav/get
        return infoService.get(reqVO);
    }

    @Override
    @TokenAuth
    public InavRespVO adminGet(GetInavVO reqVO) {
        // /v4/inav/admin/get
        return infoService.adminGet(reqVO);
    }

    @Override
    @VssTokenAuth
    public Boolean setDocumentStatus(SetDocumentStatusReqVO reqVO) {
        if (reqVO.getStatus() != null && reqVO.getStatus() != 1 && reqVO.getStatus() != 0) {
            throw new BusinessException(BizErrorCode.COMP_TAG_STATUS_NOT_ALLOWED);
        }
        return infoService.setDocumentStatus(reqVO);
    }

    private RoomsEntityDTO checkRoomIsRight(String roomId) {
        RoomsEntityDTO room = roomInnerFacade.getRoomInfo(roomId);
        if (room == null) {
            throw new BusinessException(BizErrorCode.EMPTY_ROOM);
        }
        return room;
    }

    @VssTokenAuth
    @Override
    public JSONObject preBreakSpeak(PreBreakSpeakReqVO reqVO) {
        VssToken token = VssTokenThreadLocal.getToken();
        inavService.preBreakSpeak(reqVO, token.getThirdPartyUserId());
        return new JSONObject();
    }

    @VssTokenAuth
    @Override
    public JSONObject unableSpeak(UnableSpeakReqVO reqVO) {
        VssToken token = VssTokenThreadLocal.getToken();
        inavService.unableSpeak(reqVO, token.getThirdPartyUserId());
        return new JSONObject();
    }
    @TokenAuth
    @Override
    public UserGuideInfoResponseVO getUserGuideInfo(UserGuideInfoRequestVO requestVO) {
        return inavService.getUserGuideInfo(requestVO);
    }
    @TokenAuth
    @Override
    public Boolean followGuoxinZone(FollowGuoxinZoneRequestVO vo) {
        return inavService.followGuoxinZone(vo);
    }
    @TokenAuth
    @Override
    public Boolean reportGuideShown(ReportGuideShownRequestVO vo) {
        return inavService.reportGuideShown(vo);
    }
}
